<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	//统计危急值数据
	
	String date = input.getDesc("start_date","2018-01-29");//开始时间
	String endDa = input.getDesc("end_date","2018-01-29");//结束时间
	//date= "2018-01-23";
	//endDa= "2018-01-23";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+" 00:00:00";
	String endDate = nowEndDa+" 23:59:59";
	System.out.println(startDate);
	System.out.println(endDate);
	Message msg = new Message();
	StatelessSession ss2 =  DBTools.db("basedb").getSf().openStatelessSession();
	StatelessSession ss =  DBTools.db("lisdb").getSf().openStatelessSession();
	//查询所有的科室主任
	 String ksSql = "select ks.ks_mc as ejks_mc, yg.* from YY_ZGBMK yg JOIN (\n" +
        "select id,ks_mc from VW_YSBQKSDYK where  zc_id ='01'\n" +
        ") ks on ks.id =  yg.id";
	
	//查询符合条件的危急值
    String sql = "select * from danger where alertDate>='"+startDate + "' and alertDate<='"+endDate+"'";
	try {
		 ss.beginTransaction();
		
		 SQLQuery lbsqlQuery = ss.createSQLQuery(sql);
		 lbsqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         List<Map> lblist = lbsqlQuery.list();
		 
		 //科室查询
		 ss2.beginTransaction();
		  SQLQuery ksQuery = ss2.createSQLQuery(ksSql);
		 ksQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         List<Map> kslist = ksQuery.list();
		
		 Map ksMap = new HashMap();
		 for(Map map : kslist){
			 String empId = map.get("id") == null ? "0" : map.get("id").toString();
			 String name = map.get("name") == null ? " " : map.get("name").toString();//医生名字
			 empId += "__"+name;
			
			 String ejks_mc = map.get("ejks_mc") == null ? "" : map.get("ejks_mc").toString();
			
			 if("".equals(ejks_mc)){
				continue; 
			 }
			
			  
			  String empId2 = ksMap.get(ejks_mc) == null ? "" :ksMap.get(ejks_mc).toString();
			
			 if(empId2.indexOf(empId) != -1){ //存在 不重复添加
				continue; 
			 }
			 
			  if(!"".equals(empId2)){
				  empId = empId2+ "," +  empId;
			  }
			
			  if(empId.indexOf("69554 __汤成春") != -1){
				  empId =  empId.replace("69554 __汤成春","liqun__李群");
			  }
			  
			   if(empId.indexOf("69101 __张志珺") != -1){
				    empId =  empId.replace("69101 __张志珺","litl__李同磊");
				 
			  }
			    if(empId.indexOf("69216 __施瑞华") != -1){
				    empId =  empId.replace("69216 __施瑞华","hecf__何彩凤");
				 
			  }
			 
			  
		
			 ksMap.put(ejks_mc,empId);
		
		 }
		 
		
		 ss2.getTransaction().commit();
		 ss2.close();
		 
		 ss.getTransaction().commit();
		 ss.close();
		
		//处理数据
		for(Map map : lblist){
			 String deptName = map.get("申请科室") == null ? "" : map.get("申请科室").toString();
			  String userIds  = "1";
			  
			  if(!"".equals(deptName)){
				userIds= ksMap.get(deptName) == null ? "1" :  ksMap.get(deptName).toString();
				 
			  }
			  map.put("userIds" , userIds);
		}
		msg.put("denger",lblist);
    }catch (Exception e){
	    ss.getTransaction().commit();
		 ss.close();
		  ss2.getTransaction().commit();
		 ss2.close();
    }
	
	
	
	
	out.println(msg);
%>